#!/usr/bin/perl
## $Id: image_change_original_dt.pl 5 2010-08-03 19:42:24Z halyava $
## Copyright (c) 2010 Ivan Nevostruev (halyava@gmail.com)
## 
##  Permission is hereby granted, free of charge, to any person obtaining a copy
##  of this software and associated documentation files (the "Software"), to deal
##  in the Software without restriction, including without limitation the rights
##  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
##  copies of the Software, and to permit persons to whom the Software is
##  furnished to do so, subject to the following conditions:
## 
##  The above copyright notice and this permission notice shall be included in
##  all copies or substantial portions of the Software.
## 
##  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
##  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
##  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
##  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
##  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
##  THE SOFTWARE.

use strict;
use warnings;

use feature ':5.10';

use File::Find;
use File::Spec;
use Image::ExifTool;
use Sort::Naturally;

my ($folder) = @ARGV;

if (defined $folder) {
	my $images = get_all_image_files($folder);
	my $start_time = time();
	printf("%d image%s found in [$folder]\n", scalar @$images, (@$images == 1 ? '' : 's'));
	my @sorted = nsort(@$images);
	write_date_time_original(\@sorted);
	my $work_time = time() - $start_time;
	printf("done in %d:%02d\n", $work_time/60, $work_time%60);
}
else {
	print "Usage: $0 <folder>\n";
	exit(1);
}

sub get_all_image_files {
	my ($folder) = @_;
	
	my @images;
	find(
		{
			'wanted' => sub {
				my $file = $_;
				## check if file name looks like image
				if ($file =~ m{\.(?:jpg|png|gif|bmp)$}) {
					push(@images, $file);
				}
			},
			'no_chdir' => 1,
		},
		$folder
	);
	return \@images;
}

sub write_date_time_original {
	my ($images) = @_;
	
	for my $image (@$images) {
		my $exifTool = Image::ExifTool->new();
		my $image_info = $exifTool->ImageInfo($image);
		if (defined $image_info) {
			my $new_dt_original = generate_next_datetime();
			for my $field_name ('CreationTime', 'DateTimeOriginal') {
				if (defined $image_info->{$field_name}) {
					printf("file [%s]: %s replace [%s] with [%s]\n", $image, $field_name, $image_info->{$field_name}, $new_dt_original);
				}
				else {
					printf("file [%s]: %s write [%s]\n", $image, $field_name, $new_dt_original);
				}
				$exifTool->SetNewValue($field_name, $new_dt_original);
			}
			$exifTool->WriteInfo($image);
		}
		else {
			printf("can't read image info from [$image]\n");
		}
	}
}

## generate datetime string starting from about 30 days before
sub generate_next_datetime {
	
	state $current_time = time() - 30*24*60*60;

	my @dt = localtime($current_time);
	$current_time += 1;
	$dt[5] += 1900;
	$dt[4] ++;
	return sprintf("%04d:%02d:%02d %02d:%02d:%02d", reverse @dt[0..5]);
}


#perl2exe_include "Image/ExifTool/AFCP.pm";
#perl2exe_include "Image/ExifTool/AIFF.pm";
#perl2exe_include "Image/ExifTool/APE.pm";
#perl2exe_include "Image/ExifTool/APP12.pm";
#perl2exe_include "Image/ExifTool/ASF.pm";
#perl2exe_include "Image/ExifTool/BigTIFF.pm";
#perl2exe_include "Image/ExifTool/BMP.pm";
#perl2exe_include "Image/ExifTool/BuildTagLookup.pm";
#perl2exe_include "Image/ExifTool/BZZ.pm";
#perl2exe_include "Image/ExifTool/Canon.pm";
#perl2exe_include "Image/ExifTool/CanonCustom.pm";
#perl2exe_include "Image/ExifTool/CanonRaw.pm";
#perl2exe_include "Image/ExifTool/CanonVRD.pm";
#perl2exe_include "Image/ExifTool/CaptureOne.pm";
#perl2exe_include "Image/ExifTool/Casio.pm";
#perl2exe_include "Image/ExifTool/Charset/Arabic.pm";
#perl2exe_include "Image/ExifTool/Charset/Baltic.pm";
#perl2exe_include "Image/ExifTool/Charset/Cyrillic.pm";
#perl2exe_include "Image/ExifTool/Charset/Greek.pm";
#perl2exe_include "Image/ExifTool/Charset/Hebrew.pm";
#perl2exe_include "Image/ExifTool/Charset/JIS.pm";
#perl2exe_include "Image/ExifTool/Charset/Latin.pm";
#perl2exe_include "Image/ExifTool/Charset/Latin2.pm";
#perl2exe_include "Image/ExifTool/Charset/MacArabic.pm";
#perl2exe_include "Image/ExifTool/Charset/MacChineseCN.pm";
#perl2exe_include "Image/ExifTool/Charset/MacChineseTW.pm";
#perl2exe_include "Image/ExifTool/Charset/MacCroatian.pm";
#perl2exe_include "Image/ExifTool/Charset/MacCyrillic.pm";
#perl2exe_include "Image/ExifTool/Charset/MacGreek.pm";
#perl2exe_include "Image/ExifTool/Charset/MacHebrew.pm";
#perl2exe_include "Image/ExifTool/Charset/MacIceland.pm";
#perl2exe_include "Image/ExifTool/Charset/MacJapanese.pm";
#perl2exe_include "Image/ExifTool/Charset/MacKorean.pm";
#perl2exe_include "Image/ExifTool/Charset/MacLatin2.pm";
#perl2exe_include "Image/ExifTool/Charset/MacRoman.pm";
#perl2exe_include "Image/ExifTool/Charset/MacRomanian.pm";
#perl2exe_include "Image/ExifTool/Charset/MacRSymbol.pm";
#perl2exe_include "Image/ExifTool/Charset/MacThai.pm";
#perl2exe_include "Image/ExifTool/Charset/MacTurkish.pm";
#perl2exe_include "Image/ExifTool/Charset/ShiftJIS.pm";
#perl2exe_include "Image/ExifTool/Charset/Symbol.pm";
#perl2exe_include "Image/ExifTool/Charset/Thai.pm";
#perl2exe_include "Image/ExifTool/Charset/Turkish.pm";
#perl2exe_include "Image/ExifTool/Charset/Vietnam.pm";
#perl2exe_include "Image/ExifTool/Charset.pm";
#perl2exe_include "Image/ExifTool/DICOM.pm";
#perl2exe_include "Image/ExifTool/DjVu.pm";
#perl2exe_include "Image/ExifTool/DNG.pm";
#perl2exe_include "Image/ExifTool/EXE.pm";
#perl2exe_include "Image/ExifTool/Exif.pm";
#perl2exe_include "Image/ExifTool/Fixup.pm";
#perl2exe_include "Image/ExifTool/FLAC.pm";
#perl2exe_include "Image/ExifTool/Flash.pm";
#perl2exe_include "Image/ExifTool/FlashPix.pm";
#perl2exe_include "Image/ExifTool/Font.pm";
#perl2exe_include "Image/ExifTool/FotoStation.pm";
#perl2exe_include "Image/ExifTool/FujiFilm.pm";
#perl2exe_include "Image/ExifTool/Geotag.pm";
#perl2exe_include "Image/ExifTool/GeoTiff.pm";
#perl2exe_include "Image/ExifTool/GIF.pm";
#perl2exe_include "Image/ExifTool/GPS.pm";
#perl2exe_include "Image/ExifTool/H264.pm";
#perl2exe_include "Image/ExifTool/HP.pm";
#perl2exe_include "Image/ExifTool/HTML.pm";
#perl2exe_include "Image/ExifTool/HtmlDump.pm";
#perl2exe_include "Image/ExifTool/ICC_Profile.pm";
#perl2exe_include "Image/ExifTool/ID3.pm";
#perl2exe_include "Image/ExifTool/InDesign.pm";
#perl2exe_include "Image/ExifTool/IPTC.pm";
#perl2exe_include "Image/ExifTool/ITC.pm";
#perl2exe_include "Image/ExifTool/iWork.pm";
#perl2exe_include "Image/ExifTool/JPEG.pm";
#perl2exe_include "Image/ExifTool/Jpeg2000.pm";
#perl2exe_include "Image/ExifTool/JPEGDigest.pm";
#perl2exe_include "Image/ExifTool/JVC.pm";
#perl2exe_include "Image/ExifTool/Kodak.pm";
#perl2exe_include "Image/ExifTool/KyoceraRaw.pm";
#perl2exe_include "Image/ExifTool/Lang/cs.pm";
#perl2exe_include "Image/ExifTool/Lang/de.pm";
#perl2exe_include "Image/ExifTool/Lang/en_ca.pm";
#perl2exe_include "Image/ExifTool/Lang/en_gb.pm";
#perl2exe_include "Image/ExifTool/Lang/es.pm";
#perl2exe_include "Image/ExifTool/Lang/fr.pm";
#perl2exe_include "Image/ExifTool/Lang/it.pm";
#perl2exe_include "Image/ExifTool/Lang/ja.pm";
#perl2exe_include "Image/ExifTool/Lang/ko.pm";
#perl2exe_include "Image/ExifTool/Lang/nl.pm";
#perl2exe_include "Image/ExifTool/Lang/pl.pm";
#perl2exe_include "Image/ExifTool/Lang/ru.pm";
#perl2exe_include "Image/ExifTool/Lang/sv.pm";
#perl2exe_include "Image/ExifTool/Lang/tr.pm";
#perl2exe_include "Image/ExifTool/Lang/zh_cn.pm";
#perl2exe_include "Image/ExifTool/Lang/zh_tw.pm";
#perl2exe_include "Image/ExifTool/Leaf.pm";
#perl2exe_include "Image/ExifTool/LNK.pm";
#perl2exe_include "Image/ExifTool/M2TS.pm";
#perl2exe_include "Image/ExifTool/MakerNotes.pm";
#perl2exe_include "Image/ExifTool/Matroska.pm";
#perl2exe_include "Image/ExifTool/MIE.pm";
#perl2exe_include "Image/ExifTool/MIEUnits.pod";
#perl2exe_include "Image/ExifTool/MIFF.pm";
#perl2exe_include "Image/ExifTool/Minolta.pm";
#perl2exe_include "Image/ExifTool/MinoltaRaw.pm";
#perl2exe_include "Image/ExifTool/MNG.pm";
#perl2exe_include "Image/ExifTool/MPC.pm";
#perl2exe_include "Image/ExifTool/MPEG.pm";
#perl2exe_include "Image/ExifTool/MPF.pm";
#perl2exe_include "Image/ExifTool/MWG.pm";
#perl2exe_include "Image/ExifTool/Nikon.pm";
#perl2exe_include "Image/ExifTool/NikonCapture.pm";
#perl2exe_include "Image/ExifTool/NikonCustom.pm";
#perl2exe_include "Image/ExifTool/Olympus.pm";
#perl2exe_include "Image/ExifTool/OOXML.pm";
#perl2exe_include "Image/ExifTool/Panasonic.pm";
#perl2exe_include "Image/ExifTool/PanasonicRaw.pm";
#perl2exe_include "Image/ExifTool/PDF.pm";
#perl2exe_include "Image/ExifTool/Pentax.pm";
#perl2exe_include "Image/ExifTool/PhotoMechanic.pm";
#perl2exe_include "Image/ExifTool/Photoshop.pm";
#perl2exe_include "Image/ExifTool/PICT.pm";
#perl2exe_include "Image/ExifTool/PNG.pm";
#perl2exe_include "Image/ExifTool/PostScript.pm";
#perl2exe_include "Image/ExifTool/PPM.pm";
#perl2exe_include "Image/ExifTool/PrintIM.pm";
#perl2exe_include "Image/ExifTool/PSP.pm";
#perl2exe_include "Image/ExifTool/QuickTime.pm";
#perl2exe_include "Image/ExifTool/Rawzor.pm";
#perl2exe_include "Image/ExifTool/README";
#perl2exe_include "Image/ExifTool/Real.pm";
#perl2exe_include "Image/ExifTool/Ricoh.pm";
#perl2exe_include "Image/ExifTool/RIFF.pm";
#perl2exe_include "Image/ExifTool/RSRC.pm";
#perl2exe_include "Image/ExifTool/RTF.pm";
#perl2exe_include "Image/ExifTool/Samsung.pm";
#perl2exe_include "Image/ExifTool/Sanyo.pm";
#perl2exe_include "Image/ExifTool/Shift.pl";
#perl2exe_include "Image/ExifTool/Shortcuts.pm";
#perl2exe_include "Image/ExifTool/Sigma.pm";
#perl2exe_include "Image/ExifTool/SigmaRaw.pm";
#perl2exe_include "Image/ExifTool/Sony.pm";
#perl2exe_include "Image/ExifTool/SonyIDC.pm";
#perl2exe_include "Image/ExifTool/Stim.pm";
#perl2exe_include "Image/ExifTool/TagInfoXML.pm";
#perl2exe_include "Image/ExifTool/TagLookup.pm";
#perl2exe_include "Image/ExifTool/TagNames.pod";
#perl2exe_include "Image/ExifTool/Unknown.pm";
#perl2exe_include "Image/ExifTool/Vorbis.pm";
#perl2exe_include "Image/ExifTool/WriteCanonRaw.pl";
#perl2exe_include "Image/ExifTool/WriteExif.pl";
#perl2exe_include "Image/ExifTool/WriteIPTC.pl";
#perl2exe_include "Image/ExifTool/WritePDF.pl";
#perl2exe_include "Image/ExifTool/WritePhotoshop.pl";
#perl2exe_include "Image/ExifTool/WritePNG.pl";
#perl2exe_include "Image/ExifTool/WritePostScript.pl";
#perl2exe_include "Image/ExifTool/Writer.pl";
#perl2exe_include "Image/ExifTool/WriteXMP.pl";
#perl2exe_include "Image/ExifTool/XMP.pm";
#perl2exe_include "Image/ExifTool/XMP2.pm";
#perl2exe_include "Image/ExifTool/ZIP.pm";
#perl2exe_include "Image/ExifTool.pm";
